<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="../asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script src="../asserts/plugins/jquery-3.6.0.min.js"></script>
    <script src="../asserts/plugins/bootstrap/js/bootstrap.js"></script>
    <script>
        $(function () {
            // 当前页
            let currentPage = 1;
            // 每页几条数据
            let len = 9;
            //通用：上一页、下一页、跳转指定页
            $("#next_btn").click(function () {
                currentPage += 1;
                len = $("input[name=len]").val();
                getCommoditys()
            });
            $("#prior_btn").click(function () {
                currentPage -= 1;
                len = $("input[name=len]").val();
                getCommoditys()
            })
            $("#goPage_btn").click(function () {
                currentPage = $("input[name=goPage]").val();
                len = $("input[name=len]").val();
                getCommoditys()
            });


            //判断是否卖家
            $.ajax({
                url: "/KunKunMarket/UserController?type=getMyData",
                type:"get",
                dataType: "json",
                success:function (respBean) {
                    if (respBean.status == 500){
                        alert(respBean.msg);
                        return;
                    }
                    if (respBean.data.user_type == 1){
                        // 是卖家，显示 ”我的店铺“ 超链接
                        $("#myCommodity").attr('style','display: inline-block');
                    }
                }
            })

            // 获取展示所有商品
            function getCommoditys(){
                let $commodityList = $("#commodityList").text("");
                $.ajax({
                    url:'/KunKunMarket/CommodityController',
                    type:'get',
                    dataType:"json",
                    data:{
                        type: 'getCommodityPageList',
                        currentPage: currentPage,
                        len:len
                    },
                    success:function (respBean) {
                        if (respBean.status == 500){
                            alert(respBean.msg);
                            return;
                        }
                        //更新前端分页组件信息
                        currentPage = respBean.data.currentPage;
                        len = respBean.data.len;
                        $("#currentPage").text(currentPage);
                        $("input[name=len]").text(len);


                        //获取商品，添加到元素中
                        for (let i=0; i<respBean.data.data.length; i++){
                            //创建html标签元素
                            $commodity = $(`
                            <div class="col-lg-3 col-md-4 col-sm-6 col-xs-6">
                                <div class="thumbnail">
                                    <a href="#" id="img_a">
                                        <img id="commodity_img" style="width: 255px;height: 290px" src="../asserts/img/1673342375030323477.jpg" alt="..."class="img-responsive">
                                    </a>
                                    <div class="caption">
                                        <h4 id="commodity_name"  >华为畅享7</h4>
                                        <p style="color:red;">
                                            <span>价格：</span> <span id="commodity_price" ></span>
                                        </p>
                                        <p id="commodity_stock" style="display: none"> </p>
                                        <p id="commodity_id" style="color:red;display: none"></p>
                                        <p id="commodity_user_id" style="color:red;display: none"></p>
                                        <button data-target="#tanChuang"  class="addOrder_btn btn btn-default" style="width: 75px" data-toggle="modal">购买</button>
                                        <button class="addCart_btn btn btn-primary btn-xs" style="float: right;margin-top: 7px">添加到购物车</button>
                                    </div>
                                </div>
                            </div>
                            `);
                            $commodity.find("#commodity_img").attr("src",respBean.data.data[i].img_url);
                            $commodity.find("#commodity_name").text(respBean.data.data[i].name);
                            $commodity.find("#commodity_price").text(respBean.data.data[i].price);
                            $commodity.find("#commodity_stock").text(respBean.data.data[i].stock)

                            $commodity.find("#commodity_id").text(respBean.data.data[i].id)
                            $commodity.find("#commodity_user_id").text(respBean.data.data[i].user_id);
                            //href='commodityDetailed.html?id=" +respBean.data.data[i].id+ "'
                            $commodity.find("#img_a").attr("href",' commodityDetailed.html?id=' +respBean.data.data[i].id );
                            //将新创建的元素添加到商品列表容器
                            $commodityList.append($commodity);
                        }

                        // 给每个商品内的按钮添加点击事件
                        $(".addOrder_btn").click(addOrder_btn);
                        $(".addCart_btn").click(addCart_btn);
                    }
                })
            }
            getCommoditys();

            //呼出购买商品的表单时，填充数据
            function addOrder_btn(){
                //获取到商品id, 设置到表单的异常域中
                let commodity_id = $(this).parents(".caption").find("#commodity_id").text();
                $("input[name=commodity_id]").val(commodity_id);
                $("input[name=phone]").val('');
                $("input[name=address]").val('');
                $("input[name=password]").val('');
            }
            //购买商品，生成订单
            $("button[name=addOrder]").click(function () {
                $.ajax({
                    url:"/KunKunMarket/OrderController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'addOrder',
                        commodity_id: $("input[name=commodity_id]").val(),
                        phone: $("input[name=phone]").val(),
                        address: $("input[name=address]").val(),
                        password: $("input[name=password]").val()
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                        if (respBean.status == 200){
                            $(".cel").click();
                        }
                    }
                })
            })
            //添加到购物车
            function addCart_btn() {
                //获取到商品id, 添加到购物车
                let commodity_id = $(this).parents(".caption").find("#commodity_id").text();
                $.ajax({
                    url:"/KunKunMarket/CartController",
                    type:"get",
                    dataType:"json",
                    data: {
                        type:'addCart',
                        commodity_id: commodity_id
                    },
                    success:function (respBean) {
                        alert(respBean.msg);
                    }
                })
            }

        })
    </script>
</head>
<body>
<div class="container">
    <a href="/KunKunMarket/UserController?type=logout">退出登录</a>
    <!--  导航栏  -->
    <div class="row">
        <div class="col-md-6">
            <a href="../user/userIndex.html" class="btn btn-primary btn-sm">个人资料</a>
            <a href="../user/order.html" class="btn btn-primary btn-sm">我的订单</a>
            <a href="../user/myCart.html" class="btn btn-primary btn-sm">我的购物车</a>
            <a href="../user/myCommodityList.html" style="display: none" id="myCommodity" class="btn btn-primary btn-sm">我的店铺</a>
        </div>
        <div class="col-md-6" style="text-align: right">
            <button id="prior_btn" class="btn btn-default btn-sm">上一页</button>
            <span id="currentPage"></span>
            <button id="next_btn" class="btn btn-default btn-sm">下一页</button>
            <button id="goPage_btn" class="btn btn-default btn-sm">跳转到</button>
            <input type="number" name="goPage" value="1" style="display: inline-block;width: 50px;margin-right: 10px">
            每页 <input type="number" name="len" value="9" style="display: inline-block;width: 50px"> 条数据
        </div>
    </div>

    <!-- 张略缩图开始 -->
    <div class="row" id="commodityList">

    </div>



    <!--fade 弹出表单-->
    <div class="modal fade"  id="tanChuang" aria-labelledby="myModallabel" aria-hidden="true" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h5 class="modal-title" id="myModallabel">请填写收货信息</h5>
                </div>
                <form id="formData" class="form-horizontal">
                    <input type="hidden" name="commodity_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="control-label col-lg-2">密码：</label>
                            <div class="col-lg-9">
                                <input type="password" value="" name="password" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">电话：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="phone" class="form-control"></br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-lg-2">地址：</label>
                            <div class="col-lg-9">
                                <input type="text" value="" name="address" class="form-control">
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button class="cel btn btn-default" type="button" data-dismiss="modal">取消</button>
                        <button class="btn btn-success" type="button" name="addOrder">确认</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>



</body>
</html>